$(document).ready(function(){
    
    $(".cep").blur(function(){
         if($(".cep").val().length > 0){
            $(".cep").after('<span class="ajimg"></span>');
            $.post(URLBASE+'/funcoes/ajxcep/',{ cep: this.value }, function(data){
                //if(data.list['logradouro'] > ''){
                    $(".res_uf").val(data.list['uf']);
                    $(".res_uf").change();
                    $(".res_endereco").val(data.list['logradouro']+', ');
                    $(".res_bairro").val(data.list['bairro']);
                    $(".res_endereco").focus();     
                    setTimeout(function(){ $(".res_cidade").val(data.list['cidade']); },1000);
                //}
                $(".ajimg").remove();
                
            }, 'json');
        }
    });
    
    $('.m_i_exit').click(function(){
        var msg = 'Deseja sair do sistema?';
        $('#gw-modal-msg').remove();
        $('body').append('<div id="gw-modal-msg" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h4 class="modal-title" id="mySmallModalLabel">Mensagem</h4></div><div class="modal-body">'+msg+'</div>\n\<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button><button type="button" class="btn btn-primary btn-m_i_exit">Sair</button></div></div></div></div>');
        $('#gw-modal-msg').modal('show');     
    });
    $(document).on('click', '.btn-m_i_exit', function() {
        $('#gw-modal-msg').modal('hide'); 
        location = URLBASE+'/auth/logout'; 
    });
    
});

function dataPicker(el)
{
    el.datepicker({
        //showOn: 'both',//"button",
        //buttonImage: URLSITE +"/public/media/images/sys/calendario.png",
        //buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'dd/mm/yy',
        dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'],
        dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
        dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
        monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
        monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'],
        nextText: 'Próximo',
        prevText: 'Anterior'
    });
}

function modal(){
    $('#gw-modal').remove();
    $('body').append('<div id="gw-modal" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"><div class="modal-dialog modal-sm"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h6 class="modal-title" id="mySmallModalLabel">&nbsp</h6></div><div class="modal-body">Processando...</div></div></div></div>');
    $('#gw-modal').modal('show');
    setTimeout(function(){ remove_modal(); },90000);
}
function remove_modal(){
    $('#gw-modal').modal('hide');
}

function modal_msg(msg){
    $('#gw-modal-msg').remove();
    $('body').append('<div id="gw-modal-msg" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h4 class="modal-title" id="mySmallModalLabel">Mensagem</h4></div><div class="modal-body">'+msg+'</div></div></div></div>');
    $('#gw-modal-msg').modal('show');
    setTimeout(function(){ remove_modal_msg(); },90000); 
}
function remove_modal_msg(){
    $('#gw-modal-msg').modal('hide');
}

function modal_msg_btn(msg,btn,btn2,ttl){
    if(ttl == '' || ttl === undefined) ttl = 'Mensagem';
    if(btn == '' || btn === undefined) btn = 'Confirmar';
    if(btn2 == '' || btn2 === undefined) btn2 = 'Cancelar';
    $('#gw-modal-msg-btn').remove();
    $('body').append('<div id="gw-modal-msg-btn" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><h4 class="modal-title" id="mySmallModalLabel">'+ttl+'</h4></div><div class="modal-body">'+msg+'</div>\n\<div class="modal-footer"><button type="button" class="btn btn-default btn-modal_msg_btn2" data-dismiss="modal">'+btn2+'</button><button type="button" class="btn btn-primary btn-modal_msg_btn">'+btn+'</button></div></div></div></div>');
    $('#gw-modal-msg-btn').modal('show');
}
function remove_modal_msg_btn(){
    $('#gw-modal-msg-btn').modal('hide');
}

